let logger = require("pomelo-logger").getLogger(__filename);
let path = require("path");
let express = require("express");
let accountRoute = require("./router/account");
let mailRoute = require("./router/mail");
let permissionRoute = require("./router/permission");
let headlineRoute = require("./router/headline");
let serverRoute = require("./router/server");
let gameRoute = require("./router/game");
let swiperRoute = require("./router/swiper");
let activityRoute = require("./router/activity");
let rechargeRoute = require("./router/recharge");
let announcementRoute = require("./router/announcement");
let configRoute = require("./router/config");
let expressApp = express();

module.exports = function (app) {
    return function () {
        initialize(app);
    };
};

function initialize(app) {
    app.set('proxyConfig', {
        retryTimes: -1
    });

    app.loadConfig("gmConfig", path.join(app.getBase(), "/config/gm.json"));

    // for test for wyw H5 game
    expressApp.all('*', function(req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
        res.header("X-Powered-By",' 3.2.1')
        res.header("Content-Type", "application/json;charset=utf-8");
        next();
    });

    expressApp.use("/account", accountRoute);
    expressApp.use("/mail", mailRoute);
    expressApp.use("/account", permissionRoute);
    expressApp.use("/headline", headlineRoute);
    expressApp.use("/server", serverRoute);
    expressApp.use("/game", gameRoute);
    expressApp.use("/swiper", swiperRoute);
    expressApp.use("/activity", activityRoute);
    expressApp.use("/recharge", rechargeRoute);
    expressApp.use("/announcement", announcementRoute);
    expressApp.use("/config", configRoute);

    let port = app.get("gmConfig")["httpPort"];
    logger.info("gm server listen http at " + port);
    expressApp.listen(port);
}